

document.getElementById('getCookies').addEventListener('click', function () {
  sendMessageToBackground("getCookies");
});

document.getElementById('getAllCookies').addEventListener('click', function () {
  sendMessageToBackground("getAllCookies");
});


function sendMessageToBackground(action) {
  chrome.tabs.query({active: true, currentWindow: true}, function (tabs) {
    chrome.runtime.sendMessage({action: action, url: tabs[0].url}, function (response) {
      if (action === "getCookies") {
        let container = document.getElementById('cookiesContainer');
        container.innerHTML = ''; // 清空容器
        document.getElementById('cookies').value = '';
        document.getElementById('cookies').style.display = 'none';
        let formattedCookies = formatCookies(response);
        container.appendChild(formattedCookies);
      } else if (action === "getAllCookies") {
        document.getElementById('cookiesContainer').innerHTML = '';
        document.getElementById('cookies').style.display = 'block';
        if (joinCookies(response)) {
            document.getElementById('cookies').value = joinCookies(response);
        }else {
          document.getElementById('cookies').value = "无cookie";
        }
      }
    });
  })
}

let out_cookies = [];
function formatCookies(cookies) {
  if (cookies.length === 0) {
    let cookieElement = document.createElement('div');
    cookieElement.innerHTML = `
      <div class="multi_class">
        <div class="multi_class_one"> 
          <input class="span_class" value="无cookie"/>
        </div>
      </div>`;
    return cookieElement;
  }
  out_cookies = cookies;
  let container = document.createElement('div');
  for (let i = 0; i < cookies.length; i++) {
    let cookieElement = document.createElement('div');
    cookieElement.innerHTML = `
      <div class="multi_class">
        <div class="multi_class_one"> 
          <input class="span_class" value="${cookies[i].name}"/>
          <button class="button_style_list_two">复制</button>
        </div>
      </div>`;
    // 为按钮添加事件监听器
    let button = cookieElement.querySelector('.button_style_list_two');
    button.addEventListener('click', function() {
      copyToBord(i);
    });
    container.appendChild(cookieElement);
  }
  return container;
}



function joinCookies(cookies) {
  const join = cookies.map(cookie => `${cookie.name}=${cookie.value}`).join('; ');
  const cookiesElement = document.getElementById('cookies');

  // 检查是否已经添加了事件监听器
  if (!cookiesElement.hasAttribute('data-listener-added')) {
    cookiesElement.addEventListener('click', function () {
      navigator.clipboard.writeText(join).then(function() {
        showCustomAlert('复制成功', 1000)
      }, function(err) {
        showCustomAlert('复制失败', 1000)
      });
    });

    // 标记已添加监听器
    cookiesElement.setAttribute('data-listener-added', 'true');
  }

  return join;
}


function copyToBord(i) {
  const name = out_cookies[i].name;
  const value = out_cookies[i].value;
  const str = name + "=" + value;
  navigator.clipboard.writeText(str).then(function() {
    showCustomAlert('复制成功', 1000)
  }, function(err) {
    showCustomAlert('复制失败', 1000)
  });
}


function showCustomAlert(message, duration) {
  const alertBox = document.getElementById('customAlert');
  const alertText = document.getElementById('customAlertText');
  alertText.textContent = message;
  alertBox.style.display = 'block';

  // 设置定时器关闭弹窗
  setTimeout(function() {
    alertBox.style.display = 'none';
  }, duration);
}
// 监听如果页面打开就触发getALLCookies
document.addEventListener('DOMContentLoaded', function () {
  sendMessageToBackground("getAllCookies");
});
